package com.zhny.project.txt.targetset.controller;

import com.zhny.common.utils.poi.ExcelUtil;
import com.zhny.framework.aspectj.lang.annotation.Log;
import com.zhny.framework.aspectj.lang.enums.BusinessType;
import com.zhny.framework.web.controller.BaseController;
import com.zhny.framework.web.domain.AjaxResult;
import com.zhny.framework.web.page.TableDataInfo;
import com.zhny.project.base.spevalue.domain.SpevalueType;
import com.zhny.project.base.spevalue.service.ISpevalueTypeService;
import com.zhny.project.txt.dataset.mapper.TxtDatasetMapper;
import com.zhny.project.txt.indexset.domain.TxtIndexset;
import com.zhny.project.txt.indexset.mapper.TxtIndexsetMapper;
import com.zhny.project.txt.targetset.service.ITargetsetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 特征工程指标Controller
 *
 * @author yanqun
 * @date 2019-12-11
 */
@Controller
@RequestMapping("/txt/targetset")
public class TargetsetController extends BaseController
{
    private String prefix = "txt/targetset";

    @Autowired
    private ITargetsetService targetsetService;
    @Autowired
    private TxtIndexsetMapper txtIndexsetMapper;
    @Autowired
    private TxtDatasetMapper txtDatasetMapper;
    @Autowired
    private ISpevalueTypeService spevalueTypeService;


    @RequiresPermissions("txt:targetset:view")
    @GetMapping()
    public String targetset(Model model, String datasetKey, String datasetId)
    {
        model.addAttribute("datasetKey",datasetKey);
        return prefix + "/targetset";
    }

    /**
     * 查询特征工程指标列表
     */
    @RequiresPermissions("txt:targetset:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TxtIndexset txtIndexset)
    {
        startPage();
        List<TxtIndexset> list = targetsetService.selectTargetsetList(txtIndexset);
        return getDataTable(list);
    }

    /**
     * 导出特征工程指标列表
     */
    @RequiresPermissions("txt:targetset:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TxtIndexset txtIndexset)
    {
        List<TxtIndexset> list = targetsetService.selectTargetsetList(txtIndexset);
        ExcelUtil<TxtIndexset> util = new ExcelUtil<TxtIndexset>(TxtIndexset.class);
        return util.exportExcel(list, "targetset");
    }

    /**
     * 新增特征工程指标
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存特征工程指标
     */
    @RequiresPermissions("txt:targetset:add")
    @Log(title = "特征工程指标", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TxtIndexset txtIndexset)
    {
        return toAjax(targetsetService.insertTargetset(txtIndexset));
    }

    /**
     * 修改特征工程指标
     */
    @GetMapping("/edit/{indexsetKey}")
    public String edit(@PathVariable("indexsetKey") String indexsetKey, ModelMap mmap)
    {
        TxtIndexset txtIndexset = targetsetService.selectTargetsetById(indexsetKey);
        mmap.put("targetset", txtIndexset);
        return prefix + "/edit";
    }

    /**
     * 修改保存特征工程指标
     */
    @RequiresPermissions("txt:targetset:edit")
    @Log(title = "特征工程指标", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TxtIndexset txtIndexset)
    {
        return toAjax(targetsetService.updateTargetset(txtIndexset));
    }

    /**
     * 删除特征工程指标
     */
    @RequiresPermissions("txt:targetset:remove")
    @Log(title = "特征工程指标", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(targetsetService.deleteTargetsetByIds(ids));
    }

    //判断数据集有没有被做过质量值
    @PostMapping("/checkQualityValue")
    @ResponseBody
    public Map<String,Object> checkQualityValue(String key){
        Map<String,Object> map = new HashMap<String,Object>();
        int flag = 0 ;
        TxtIndexset infor = txtIndexsetMapper.selectDisposeFlag(key);
        if(("3").equals(infor.getDisposeFlag()) ){
            flag =  1;  //已经做过质量值
        }else {
            flag = 0; //没做过质量值
        }
        map.put("flag",flag);
        return  map;
    }
     //质量值计算  更新
    @GetMapping("/acope/{indexsetKey}/{datasetKey}/{englishName}")
    public String acope(@PathVariable("indexsetKey") String indexsetKey, ModelMap mmap,@PathVariable("datasetKey") String datasetKey,
                        @PathVariable ("englishName") String englishName)
    {
        List<SpevalueType> spevalueTypeList = spevalueTypeService.selectSpevalueTypeList(new SpevalueType());
        mmap.put("spevalueTypeList",spevalueTypeList);
        mmap.put("indexsetKey",indexsetKey);
        mmap.put("datasetKey",datasetKey);
        mmap.put("englishName",englishName);
        return prefix + "/qualitySelect";
    }
//    @RequiresPermissions("deal:quotaset:edit")
    @Log(title = "指标集", businessType = BusinessType.UPDATE)
    @PostMapping("/qualitySelect")
    @ResponseBody
    public AjaxResult editSaverange(TxtIndexset txtIndexset)
    {

        return toAjax(targetsetService.updateRange(txtIndexset));
    }


    //特征值处理

    @GetMapping("/boxs/{datasetKey}")
    public String boxs(@PathVariable("datasetKey")  String datasetKey, ModelMap mmap)
    {
        mmap.put("datasetKey",datasetKey);
        List<SpevalueType> spevalueTypeList = spevalueTypeService.selectSpevalueTypeList(new SpevalueType());
        mmap.put("spevalueTypeList",spevalueTypeList);
        return prefix + "/boxs";
    }

    /**
     * 修改保存特征值
     */
//    @RequiresPermissions("deal:quotaset:edit")
    @Log(title = "指标集", businessType = BusinessType.UPDATE)
    @PostMapping("/editValue")
    @ResponseBody
    public AjaxResult editValue(TxtIndexset txtIndexset)
    {

        return toAjax(targetsetService.editValue(txtIndexset));
    }


}
